package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.CarT;
import com.btkjgc.models.extend.CarModel;
import com.btkjgc.service.impl.CarListManageService_Impl;
import com.btkjgc.service.intf.CarListManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class CarListManageController
{
	static Logger logger = Logger.getLogger(CarListManageController.class);
	
	@Autowired
	private CarListManageService_Intf carListManageService;
	
	@RequestMapping(value = "car_list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request) 
	{
		ModelAndView mv = new ModelAndView("car_list");
		listrecord(request,mv);
		return mv;
	}
	
	@RequestMapping(value = "get_car_by_id")
	@ResponseBody
	public CarModel getCarById(Integer id) {
		
		String sql = "SELECT * FROM car_t WHERE id = ?";
		
		CarModel carModel = (CarModel) HibernateUtil
				.executeSQLQueryObject(sql, CarModel.class, id);
		
		return carModel;
	}
	
	@RequestMapping(value = "add_car", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String addCar(CarModel carModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException{
		
		System.out.println("AddCar"); 
		CarT car = new CarT();
		car.setCarplate(carModel.getCarplate());
		car.setFramenumber(carModel.getFramenumber());
		car.setCartypeid(carModel.getCartypeid());
		car.setBusiness_type(carModel.getBusiness_type());
		car.setColor(carModel.getColor());
		car.setCreatedate(new Date());
		car.setDel(0);
		car.setPlatedate(carModel.getPlatedate());
		car.setCompanyid(carModel.getCompanyid());
		car.setFree(carModel.getFree());
		car.setFirstfee((carModel.getFirstfee()!=null? carModel.getFirstfee():0));
		car.setLastfee((carModel.getLastfee()!=null? carModel.getLastfee():0));
		carListManageService.addCar(car);
		
		return "redirect:/car_list";
	}
	
	@RequestMapping(value = "edit_car", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String editCar(CarModel carModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException{
		
		System.out.println("EditCar");
		carListManageService.updateCar(carModel);
		
		return "redirect:/car_list";
	}
	
	@RequestMapping(value = "update_carplate", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String updateCarplate(CarModel carModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException{
		
		System.out.println("UpdateCarplate");
		carListManageService.updateCarplate(carModel);
		
		return "redirect:/car_list";
	}

	@SuppressWarnings("unchecked")
	private void listrecord(HttpServletRequest request, ModelAndView mv) 
	{
		carListManageService = new CarListManageService_Impl();
		
		String carplate = request.getParameter("carplate");
		String framenumber = request.getParameter("framenumber");
		String cartypeid = request.getParameter("cartypeid");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1={"carplate","like",carplate};
		Object[] arr2={"framenumber","like",framenumber};
		Object[] arr3={"cartypeid","=",cartypeid};
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		
		String sql = " SELECT\n" +
					 " ct.*,aut.company,\n" +
					 " dt.value,cat.del AS flag,\n" +
					 " ctt.`name` AS cartype\n" +
					 " FROM\n" +
					 " car_t ct\n" +
					 " LEFT JOIN account_user_t aut ON aut.id = ct.companyid\n" +
					 " LEFT JOIN car_account_t cat ON cat.carid = ct.id\n" +
					 " LEFT JOIN cartype_t ctt ON ctt.id = ct.cartypeid\n" +
					 "LEFT JOIN (\n" +
					 " SELECT\n" +
					 " *\n" +
					 " FROM\n" +
					 " dictionary_t\n" +
					 " WHERE\n" +
					 " NAME = 'BusinessType'\n" +
					 " ) dt ON dt.key = ct.business_type\n" +
					 " WHERE\n" +
					 " ct.del = 0\n";
		
		sql = carListManageService.getwhereSQL(sql, paramList, sortfield);
		
		//获取分页查询的数据结果
		totalRecord = carListManageService.getCount(sql, paramList);
		
		List<CarModel> carModel = carListManageService.getList(
				CarModel.class, paramList, sql, pageIndex, 15, sortfield);
		
		//设置后台用户界面所需的其他数据
		carListManageService.setListData(request,mv,pageIndex,totalRecord,carModel);
		
	}
	
}